package com.common.util;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;

/**
 * Created by dell on 2016/5/11.
 */
public class SecreUtil {

    public static class SessionAtrr{

        public static  final String  VERIFYCODE="verifyCode";        //验证码

        public static  final String  EMAILVERIFYCODE="emailverifyCode";        //邮箱验证码

        public static  final String  PHONEVERIFYCODE="phoneverifyCode";        //手机验证码

        public static final  String USERMAP="userMap";

        public static final String  SYSUSERNAME="sysuername";

        public static final String TERMINAL="terminal";

    }

    /**
     * 将数据放入会话
     * @param key
     * @param value
     */
    public static void putSessionValue(Object key,Object value){
        Session session = SecurityUtils.getSubject().getSession(true);
        session.setAttribute(key,value);

    }

    /**
     * 从会话中取出数据
     * @param key
     * @return
     */
    public static Object getSessionValue(String key){

        Session session = SecurityUtils.getSubject().getSession();
        return session.getAttribute(key);
    }

    /**
     * 获取当前用户id
     * @return
     */
    public static String getUserId(){
      return  (String) SecurityUtils.getSubject().getPrincipal();
    }

    /**
     * 获取当前用户名
     * @return
     */
    public static String getUserName(){
        return (String) SecreUtil.getSessionValue(SecreUtil.SessionAtrr.SYSUSERNAME);
    }

    /**
     * 获取用户ip
     * @return
     */
    public static String getIp(){
        return SecurityUtils.getSubject().getSession().getHost();
    }

}
